window.onload = function () {
    MyToDoList();                   // 我的待办
    NoticeAnnouncement();             // 获取通知公告信息
    MyReceipt()                     // 我的收文
    GetScheduling();             // 获取排班信息
    MyMail();                    // 获取我的邮件

};


function GetScheduling() {
    var xhr = new XMLHttpRequest();
    var local_host = window.location.host;          // 获取当前IP
    xhr.open('post', "/system-setup/get_scheduling");
    xhr.setRequestHeader('content-type', 'application/x-www-form-urlencoded');
    xhr.setRequestHeader('Access-Control-Allow-Origin', '*');
    xhr.send();
    xhr.onreadystatechange = function () {
        if (xhr.readyState === 4) {
            if (xhr.status === 200) {
                document.getElementById("loading").style.display = "none";
                var JsonDate = JSON.parse(xhr.responseText);
                console.log(JsonDate)
                var all_dict = JsonDate["all_dict"];
                var one_line = all_dict["one_line"];
                var two_line = all_dict["two_line"];
                var today_day = all_dict["today"];
                var one_line_1 = document.getElementById("one_line_1");
                var one_line_2 = document.getElementById("one_line_2");
                var twoLine = document.getElementById("twoLine");
                var openDate = document.getElementById("openDate");

                var tag_id_list = ['pb_one', 'pb_two', 'pb_three', 'pb_four', 'pb_five', 'pb_six', 'pb_seven']
                var line_num = 0
                for (var pb in one_line) {
                    var tag_line = document.getElementById(tag_id_list[line_num]);
                    if (one_line[pb][0]['duty_date'] === today_day) {
                        tag_line.style.color = '#FF6347';
                    }
                    var div_week = document.createElement("div");
                    div_week.style.width = '25%';
                    div_week.style.height = '62.5px';
                    div_week.style.display = 'inline-block';
                    div_week.style.fontSize = '14px';
                    div_week.style.lineHeight = '30px';
                    div_week.style.textAlign = 'center';
                    div_week.style.borderRight = '#DCDCDC solid 1px';
                    div_week.style.verticalAlign = 'top';
                    div_week.innerHTML = one_line[pb][0]['week'] + "<br>" + one_line[pb][0]['duty_date'];
                    tag_line.appendChild(div_week);
                    if (one_line[pb].length === 1) {
                        var div_week1 = document.createElement("div");
                        div_week1.style.width = '25%';
                        div_week1.style.height = '62.5px';
                        div_week1.style.display = 'inline-block';
                        div_week1.style.fontSize = '14px';
                        div_week1.style.lineHeight = '62.5px';
                        div_week1.style.textAlign = 'center';
                        div_week1.style.borderRight = '#DCDCDC solid 1px';
                        div_week1.style.verticalAlign = 'top';
                        div_week1.innerText = "夜班";

                        var div_week4 = document.createElement("div");
                        div_week4.style.width = '25%';
                        div_week4.style.height = '62.5px';
                        div_week4.style.display = 'inline-block';
                        div_week4.style.fontSize = '14px';
                        div_week4.style.lineHeight = '62.5px';
                        div_week4.style.textAlign = 'center';
                        div_week4.style.borderRight = '#DCDCDC solid 1px';
                        div_week4.style.verticalAlign = 'top';
                        div_week4.innerText = one_line[pb][0]['name'] + "(" + one_line[pb][0]['phone'] + ")";

                        tag_line.appendChild(div_week1);
                        tag_line.appendChild(div_week4);
                    }
                    if (one_line[pb].length === 2) {
                        var div_week2 = document.createElement("div");
                        div_week2.style.width = '25%';
                        div_week2.style.height = '62.5px';
                        div_week2.style.display = 'inline-block';
                        div_week2.style.fontSize = '14px';
                        div_week2.style.lineHeight = '30px';
                        div_week2.style.textAlign = 'center';
                        div_week2.style.borderRight = '#DCDCDC solid 1px';
                        div_week2.style.verticalAlign = 'top';

                        var div_week6 = document.createElement("div");
                        div_week6.style.width = '25%';
                        div_week6.style.height = '62.5px';
                        div_week6.style.display = 'inline-block';
                        div_week6.style.fontSize = '14px';
                        div_week6.style.lineHeight = '30px';
                        div_week6.style.textAlign = 'center';
                        div_week6.style.borderRight = '#DCDCDC solid 1px';
                        div_week6.style.verticalAlign = 'top';
                        for (var hh = 0; hh < 2; hh++) {
                            var div_week3 = document.createElement("div");
                            div_week3.style.width = '100%';
                            div_week3.style.height = '31.25px';
                            if (hh === 0) {
                                div_week3.style.borderBottom = '#DCDCDC solid 1px';
                            }
                            div_week3.innerText = one_line[pb][hh]['type'];
                            div_week2.appendChild(div_week3)

                            var div_week7 = document.createElement("div");
                            div_week7.style.width = '100%';
                            div_week7.style.height = '31.25px';
                            if (hh === 0) {
                                div_week7.style.borderBottom = '#DCDCDC solid 1px';
                            }
                            div_week7.innerText = one_line[pb][hh]['name'] + "(" + one_line[pb][hh]['phone'] + ")";
                            div_week6.appendChild(div_week7)
                        }
                        tag_line.appendChild(div_week2);
                        tag_line.appendChild(div_week6);
                    }
                    var div_week5 = document.createElement("div");
                    div_week5.style.width = '25%';
                    div_week5.style.height = '62.5px';
                    div_week5.style.display = 'inline-block';
                    div_week5.style.fontSize = '14px';
                    div_week5.style.lineHeight = '62.5px';
                    div_week5.style.textAlign = 'center';
                    div_week5.style.borderRight = '#DCDCDC solid 1px';
                    div_week5.style.verticalAlign = 'top';
                    div_week5.innerText = two_line[pb][0]['name'];
                    tag_line.appendChild(div_week5);
                    line_num += 1;
                }
            }
        }
     };
}

function MyToDoList() {
    var xhr = new XMLHttpRequest();
    var local_host = window.location.host;          // 获取当前IP
    xhr.open('post', "/system-setup/my_to_do_list");
    xhr.setRequestHeader('content-type', 'application/x-www-form-urlencoded');
    xhr.setRequestHeader('Access-Control-Allow-Origin', '*');
    xhr.send();
    xhr.onreadystatechange = function () {
        if (xhr.readyState === 4) {
            if (xhr.status === 200) {
                document.getElementById("loading").style.display = "none";
                var JsonDate = JSON.parse(xhr.responseText);
                console.log(JsonDate)
                if (JsonDate["daiban_list"].length) {
                    for (var i=0; i<JsonDate["daiban_list"].length; i++) {
                        if (JsonDate["daiban_list"][i]["types"] === "用印申请") {
                            var get_url = '/personal-center/seal_apply_detail_view?id='+JsonDate["daiban_list"][i]["id"];
                        }else {
                            get_url = JsonDate["daiban_list"][i]["url"];
                        }
                        var html_str = `
                            <li style="height: 38px;line-height: 38px;list-style: none;">
                                <a href="`+get_url+`" style="width: 65%;display: inline-block;overflow: hidden;text-overflow:ellipsis;white-space: nowrap;color: #0f1317;">
                                    <span style="font-size: 20px;vertical-align: top;">></span> &nbsp;
                                    <span style="vertical-align: top;">`+JsonDate["daiban_list"][i]["title"]+`</span>
                                </a>
                                <p style="width: 33%;display: inline-block;text-align: right;vertical-align: top;">`+JsonDate["daiban_list"][i]['create_time']+`</p>
                            </li>
                        `
                        $("#ToDoList").append(html_str)
                    }
                }else {
                    html_str = `
                        <div style="width: 100%;height: 100%;text-align: center;">
                            <img src="../../static/personal_center/img/暂无资源.png" alt=""
                                 style="width: 120px;height: 120px;margin-top: 30px;">
                            <p style="color: #cfcfcf;">暂无数据</p>
                        </div>
                    `
                    $("#ToDoList").append(html_str)
                }
            }
        }
    }
}

function NoticeAnnouncement() {
    var xhr = new XMLHttpRequest();
    var local_host = window.location.host;          // 获取当前IP
    xhr.open('post', "/system-setup/notice_announcement");
    xhr.setRequestHeader('content-type', 'application/x-www-form-urlencoded');
    xhr.setRequestHeader('Access-Control-Allow-Origin', '*');
    xhr.send();
    xhr.onreadystatechange = function () {
        if (xhr.readyState === 4) {
            if (xhr.status === 200) {
                document.getElementById("loading").style.display = "none";
                var JsonDate = JSON.parse(xhr.responseText);
                console.log(JsonDate)
                if (JsonDate["notic_list"].length) {
                    for (var i=0; i<JsonDate["notic_list"].length; i++) {
                        if (JsonDate["notic_list"][i]["state"] === "accept") {
                            var html_str = `
                                <li style="height: 40px;line-height: 40px;list-style: none;position: relative;"><a
                                        href="/announcements/notice_detail.html?type=all&id=`+JsonDate["notic_list"][i]["id"]+`"
                                        style="width: 70%;display: inline-block;overflow: hidden;text-overflow:ellipsis;white-space: nowrap;color: #0f1317;"><span
                                        style="font-size: 20px;vertical-align: top;">></span> &nbsp;<span
                                        style="vertical-align: top;">`+JsonDate["notic_list"][i]["name"]+`</span></a><span><img
                                        src="../../static/personal_center/img/new.png"
                                        style="width: 30px;height: 30px;position: absolute;z-index: 10;margin-top: 5px;"
                                        alt=""></span>
                                    <p style="width: 28%;display: inline-block;text-align: right;vertical-align: top;color: #1c2838;">`+JsonDate["notic_list"][i]["create_time"]+`</p>
                                </li>
                            `
                        }else {
                            html_str = `
                                <li style="height: 40px;line-height: 40px;list-style: none;"><a
                                            href="/announcements/notice_detail.html?type=all&id=`+JsonDate["notic_list"][i]["id"]+`"
                                            style="width: 70%;display: inline-block;overflow: hidden;text-overflow:ellipsis;white-space: nowrap;color: #0f1317;"><span
                                            style="font-size: 20px;vertical-align: top;">></span> &nbsp;<span
                                            style="vertical-align: top;">`+JsonDate["notic_list"][i]["name"]+`</span></a>
                                        <p style="width: 28%;display: inline-block;text-align: right;vertical-align: top;color: #1c2838;">`+JsonDate["notic_list"][i]["create_time"]+`</p>
                                    </li>
                            `
                        }
                        $("#MyAlerts").append(html_str)
                    }
                }else {
                    html_str = `
                        <div style="width: 100%;height: 100%;text-align: center;">
                            <img src="../../static/personal_center/img/暂无资源.png" alt=""
                                 style="width: 40px;height: 40px;">
                            <p style="color: #cfcfcf;">暂无数据</p>
                        </div>
                    `
                    $("#MyAlerts").append(html_str)
                }
            }
        }
    }
}

function MyReceipt() {
    var xhr = new XMLHttpRequest();
    var local_host = window.location.host;          // 获取当前IP
    xhr.open('post', "/system-setup/my_receipt");
    xhr.setRequestHeader('content-type', 'application/x-www-form-urlencoded');
    xhr.setRequestHeader('Access-Control-Allow-Origin', '*');
    xhr.send();
    xhr.onreadystatechange = function () {
        if (xhr.readyState === 4) {
            if (xhr.status === 200) {
                document.getElementById("loading").style.display = "none";
                var JsonDate = JSON.parse(xhr.responseText);
                console.log(JsonDate)
                if (JsonDate["data_list"].length) {
                    for (var i=0; i<JsonDate["data_list"].length; i++) {
                        var html_str = `
                            <li style="height: 38px;line-height: 38px;list-style: none;"><a href="`+JsonDate["data_list"][i]["url"]+`" style="width: 70%;display: inline-block;overflow: hidden;text-overflow:ellipsis;white-space: nowrap;color: #0f1317;"><span
                                    style="font-size: 20px;vertical-align: top;">></span> &nbsp;<span
                                    style="vertical-align: top;">`+JsonDate["data_list"][i]["title"]+`</span></a>
                                <p style="width: 28%;display: inline-block;text-align: right;vertical-align: top;">`+JsonDate["data_list"][i]["create_time"]+`</p>
                            </li>
                        `
                        $("#MyDocuments").append(html_str)
                    }
                }else {
                    html_str = `
                        <div style="width: 100%;height: 100%;text-align: center;">
                            <img src="../../static/personal_center/img/暂无资源.png" alt=""
                                 style="width: 120px;height: 120px;margin-top: 30px;">
                            <p style="color: #cfcfcf;">暂无数据</p>
                        </div>
                    `
                    $("#MyDocuments").append(html_str)
                }
            }
        }
    }
}

function MyMail() {
    var xhr = new XMLHttpRequest();
    var local_host = window.location.host;          // 获取当前IP
    xhr.open('post', "/system-setup/my_mail");
    xhr.setRequestHeader('content-type', 'application/x-www-form-urlencoded');
    xhr.setRequestHeader('Access-Control-Allow-Origin', '*');
    xhr.send();
    xhr.onreadystatechange = function () {
        if (xhr.readyState === 4) {
            if (xhr.status === 200) {
                document.getElementById("loading").style.display = "none";
                var JsonDate = JSON.parse(xhr.responseText);
                console.log(JsonDate)
                if (JsonDate["inbox_li"].length) {
                    for (var i=0; i<JsonDate["inbox_li"].length; i++) {
                        console.log(JsonDate["inbox_li"][i]["read"])
                        if (JsonDate["inbox_li"][i]["read"] === 'true') {
                            var html_str = `
                                <li style="height: 40px;line-height: 40px;list-style: none;"><a
                                        href="/correspondence/inbox.html?message_id=`+JsonDate["inbox_li"][i]["message_id"]+`"
                                        style="width: 70%;display: inline-block;overflow: hidden;text-overflow:ellipsis;white-space: nowrap;color: #0f1317;"><span
                                        style="font-size: 20px;vertical-align: top;">></span> &nbsp;<span
                                        style="vertical-align: top;">`+JsonDate["inbox_li"][i]["title"]+`</span></a>
                                    <p style="width: 28%;display: inline-block;text-align: right;vertical-align: top;">`+JsonDate["inbox_li"][i]["create_time"]+`</p>
                                </li>
                            `
                        }else {
                            html_str = `
                                <li style="height: 40px;line-height: 40px;list-style: none;color: #0f1317;position: relative;">
                                    <a href="/correspondence/inbox.html?message_id=`+JsonDate["inbox_li"][i]["message_id"]+`"
                                       style="width: 70%;display: inline-block;overflow: hidden;text-overflow:ellipsis;white-space: nowrap;color: #0f1317;"><span
                                            style="font-size: 20px;vertical-align: top;">></span> &nbsp;<span
                                            style="vertical-align: top;">`+JsonDate["inbox_li"][i]["title"]+`</span></a><span><img
                                        src="../../static/personal_center/img/new.png"
                                        style="width: 30px;height: 30px;position: absolute;z-index: 10;margin-top: 5px;"
                                        alt=""></span>
                                    <p style="width: 28%;display: inline-block;text-align: right;vertical-align: top;">`+JsonDate["inbox_li"][i]["create_time"]+`</p>
                                </li>
                            `
                        }
                        $("#MyEmail").append(html_str)
                    }
                }else {
                    html_str = `
                        <div style="width: 100%;height: 100%;text-align: center;">
                            <img src="../../static/personal_center/img/暂无资源.png" alt=""
                                 style="width: 40px;height: 40px;">
                            <p style="color: #cfcfcf;">暂无数据</p>
                        </div>
                    `
                    $("#MyEmail").append(html_str)
                }
            }
        }
    }
}